package com.meiyou.controller;

import com.meiyou.dto.AddCommentDto;
import com.meiyou.dto.CommentPageDto;
import com.meiyou.result.PageResult;
import com.meiyou.result.Result;
import com.meiyou.service.CommentService;
import com.meiyou.vo.CommentVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @BelongsProject: meiyou
 * @BelongsPackage: com.meiyou.controller
 * @Author: 尹晓安
 * @Date: 2023/12/3 20:55
 * @Description: todo
 */
@RestController
@RequestMapping("/api/comment")
@Slf4j
public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     * 发布评论
     *
     * @param addCommentDto
     * @return
     */
    @PostMapping
    public Result addComment(@RequestBody AddCommentDto addCommentDto) {

        commentService.addComment(addCommentDto);

        return Result.success();
    }

    /**
     * 分页获取文章评论
     * @param articleId
     * @param commentPageDto
     * @return
     */
    @GetMapping("/{articleId}")
    public Result<PageResult> getCommentPage(@PathVariable Long articleId, CommentPageDto commentPageDto) {

        PageResult pageResult = commentService.getCommentPage(articleId, commentPageDto);

        return Result.success(pageResult);
    }

    /**
     * 分页获取文章子评论
     *
     * @param commentId
     * @param commentPageDto
     * @return
     */
    @GetMapping("/child/{commentId}")
    public Result<PageResult> getChildCommentPage(@PathVariable Long commentId, CommentPageDto commentPageDto) {

        PageResult pageResult = commentService.getChildCommentPage(commentId, commentPageDto);

        return Result.success(pageResult);
    }

    /**
     * 分页获取用户评论
     *
     * @param commentPageDto
     * @return
     */
    @GetMapping("/user")
    public Result<PageResult> getUserCommentPage(CommentPageDto commentPageDto) {

        PageResult pageResult = commentService.getUserCommentPage(commentPageDto);

        return Result.success(pageResult);
    }

    /**
     * 删除评论
     *
     * @param id 评论id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result<PageResult> getUserCommentPage(@PathVariable Long id) {

        commentService.deleteComment(id);

        return Result.success("删除成功！");
    }

    /**
     * 点赞评论
     *
     * @param id 评论id
     * @return
     */
    @PostMapping("/star/{id}")
    public Result<PageResult> starComment(@PathVariable Long id) {

        commentService.starComment(id);

        return Result.success("点赞成功！");
    }

}
